package cn.yunhe.servlet;

import cn.yunhe.entity.ProductCategory;
import cn.yunhe.service.ProductCategoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdCategoryServlet")
public class UpdCategoryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int epcid = Integer.parseInt(request.getParameter("epcid"));
        String epcname = request.getParameter("className");
        int epcpartentid = Integer.parseInt(request.getParameter("parentId"));

        ProductCategory pc = new ProductCategory();
        pc.setEpcid(epcid);
        pc.setEpcname(epcname);
        pc.setEpcpartentid(epcpartentid);

        ProductCategoryService pcs = new ProductCategoryService();
        boolean b = pcs.modifyCategory(pc);
        if(b){
            if(epcpartentid==0){
                pcs.modifyEpcpartentid(epcid);
                response.sendRedirect("manage/manage-result.jsp");
            }else {
                response.sendRedirect("manage/manage-result.jsp");
            }
        }else {
            response.sendRedirect("../GetParentsServlet");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
